package com.sgolub.commanndsonline.reports;

import java.util.ArrayList;
import java.util.List;

import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sgolub.commanndsonline.domain.entity.UserBasket;
import com.sgolub.commanndsonline.service.entity.UserBasketService;

@Service
public class JasperDatasourceService {

	@Autowired
	private UserBasketService userBasketService;
	
	/**
	 * Returns a data source that's wrapped within {@link JRDataSource}
	 * @return
	 */
	public JRDataSource getDataSource() {
		List<UserBasket> records = userBasketService.listCurrentUserBasket(3);
		List<JasperUserBasket> dtos = new ArrayList<JasperUserBasket>();
	
		// Map records
		for (UserBasket userBasket: records) {
			JasperUserBasket dto = new JasperUserBasket();
			dto.setId(userBasket.getId());
			dto.setBeginCommandDate(userBasket.getBeginCommandDate());
			dto.setEndCommandDate(userBasket.getEndCommandDate());
			
			dtos.add(dto);
		}
		// Return wrapped collection
		return new JRBeanCollectionDataSource(dtos);
	}
	
}
